/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.personnel.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.admin.api.entity.SysUserinfo;
import com.fuyao.cloud.admin.api.feign.RemoteUserInfoService;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.excel.vo.ErrorMessage;
import com.fuyao.cloud.personnel.api.entity.Politicalinfo;
import com.fuyao.cloud.personnel.api.vo.excel.PoliticalinfoExcelVO;
import com.fuyao.cloud.personnel.mapper.PoliticalinfoMapper;
import com.fuyao.cloud.personnel.service.PoliticalinfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 政治面貌信息
 *
 * @author 碎月
 * @date 2021-12-16 10:35:27
 */
@Service
public class PoliticalinfoServiceImpl extends ServiceImpl<PoliticalinfoMapper, Politicalinfo> implements PoliticalinfoService {

    @Resource
    private RemoteUserInfoService userInfoService;

    @Override
    public R importPoliticalInfo(List<PoliticalinfoExcelVO> excelVOList, BindingResult bindingResult) {
        // 通用校验获取失败的数据
        List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();
        for (int i = 0; i < excelVOList.size(); i++) {
            PoliticalinfoExcelVO infoExcelVO = excelVOList.get(i);
            Set<String> errorMsg = new HashSet<>();
            SysUserinfo userinfo = this.userInfoService.getByPersonnelNum(infoExcelVO.getPersonnelNum()).getData();
            if (userinfo == null && userinfo.getId() == null){
                errorMsg.add(String.format("人员编号不存在"));
            }
            // 数据合法情况
            if (CollUtil.isEmpty(errorMsg)) {
                insertExcelPoliticalInfo(infoExcelVO,userinfo.getId(),userinfo.getName());
            }
            else {
                // 数据不合法情况
                errorMessageList.add(new ErrorMessage((long) (i + 2), errorMsg));
            }

        }

        if (CollUtil.isNotEmpty(errorMessageList)) {
            return R.failed(errorMessageList);
        }
        return R.ok();
    }

    private void insertExcelPoliticalInfo(PoliticalinfoExcelVO infoExcelVO,Long userInfoId,String userName) {
        Politicalinfo politicalinfo =new Politicalinfo();
        BeanUtils.copyProperties(infoExcelVO,politicalinfo);
        if(StrUtil.isNotEmpty(infoExcelVO.getInDate())){
            politicalinfo.setInDate(DateUtil.parseDate(infoExcelVO.getInDate()).toLocalDateTime().toLocalDate());
        }
        politicalinfo.setUserinfoId(userInfoId);
        politicalinfo.setUserName(userName);
        this.baseMapper.insert(politicalinfo);
    }
}
